<template>
  <div
    class="components-container"
    style="height:100vh">
    <el-card>
      <div class="head-lavel">
        <div class="table-button">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="addForm=true">新建</el-button>
        </div>
        <div class="table-search">
          <el-input
            v-model="searchdata"
            placeholder="搜索 ..."
            @keyup.enter.native="searchClick">
            <i
              slot="suffix"
              class="el-icon-search el-input__icon"
              @click="searchClick"/>
          </el-input>
        </div>
      </div>
      <div>
        <el-table
          :data="tableData"
          border
          style="width: 100%">
          <el-table-column
            prop="name"
            label="名称"
            sortable="custom"/>
          <el-table-column
            prop="usergroups"
            label="用户组"/>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                type="success"
                size="small"
                @click="handleEdit(scope.row)">修改</el-button>
              <el-button
                type="danger"
                size="small"
                @click="deleteGroup(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="table-pagination">
        <el-pagination
          :current-page.sync="currentPage"
          :page-sizes="pagesize"
          :page-size="limit"
          :layout="pageformat"
          :total="tabletotal"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>
    <el-dialog
      :visible.sync="addForm"
      @close="closeDialog">
      <add-group @DialogStatus="getDialogStatus"/>
    </el-dialog>
    <el-dialog
      :visible.sync="editForm"
      @close="closeDialog">
      <edit-group
        :rowdata="rowdata"
        @DialogStatus="getDialogStatus"/>
    </el-dialog>
  </div>
</template>

<script>
import {getHostPerm, deleteHostPerm} from '@/api/perm'
import {LIMIT, pagesize, pageformat} from '@/config'
import addGroup from './components/addhostperm.vue'
import editGroup from './components/edithostperm.vue'

export default {
  components: {addGroup, editGroup},
  data() {
    return {
      tableData: [],
      tabletotal: 0,
      searchdata: '',
      currentPage: 1,
      limit: LIMIT,
      offset: '',
      pagesize: pagesize,
      pageformat: pageformat,
      addForm: false,
      editForm: false,
      rowdata: {}
    }
  },

  created() {
    this.fetchData()
  },

  methods: {
    fetchData() {
      const parms = {
        limit: this.limit,
        offset: this.offset,
        name__contains: this.searchdata
      }
      getHostPerm(parms).then(response => {
        this.tableData = response.data.results
        this.tabletotal = response.data.count
      })
    },
    getDialogStatus(data) {
      this.editForm = data
      this.addForm = data
      this.fetchData()
    },
    deleteGroup(id) {
      deleteHostPerm(id).then(response => {
        this.$message({
          message: '恭喜你，删除成功',
          type: 'success'
        })
        this.fetchData()
      }).catch(error => {
        this.$message.error('删除失败')
        console.log(error)
      })
    },
    closeDialog() {
      this.fetchData()
    },
    handleEdit(row) {
      this.editForm = true
      this.rowdata = row
    },
    searchClick() {
      this.fetchData()
    },
    handleSizeChange(val) {
      this.limit = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.offset = (val - 1) * LIMIT
      this.fetchData()
    }
  }
}
</script>

<style lang='scss'>

</style>
